home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8475 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  100 lines

  1. Path: in1.uu.net!pipeline!not-for-mail
  2. From: dkolmar@grovestocktn.com (Doug Kolmar)
  3. Newsgroups: comp.lang.c
  4. Subject: Fortran to C conversion help?
  5. Date: 4 Mar 1996 09:53:08 -0500
  6. Organization: The Pipeline
  7. Message-ID: <4hf04k$ogs@pipe3.nyc.pipeline.com>
  8. NNTP-Posting-Host: pipe3.nyc.pipeline.com
  9. X-PipeUser: dkolmar
  10. X-PipeHub: nyc.pipeline.com
  11. X-PipeGCOS: (Doug Kolmar)
  12. X-Newsreader: The Pipeline v3.3.0
  13.  
  14.  
  15. Hello folks, 
  16.  
  17. I'm sort of a weekend C programmer so perhaps I've missed something, but
  18. I'm trying to convert a function in FORTRAN to C. 
  19.  
  20. Attached is the FORTRAN code followed by the C code that I've written. The
  21. C program will compile and run, but does not yield the correct output. I.E.
  22. integers in the range 0 to 31. 
  23.  
  24. Except for the differences in the range of the RAND functions, I know
  25. nothing about FORTRAN, so am I missing something implicit in the code? Any
  26. help would be greatly appreciated. 
  27.  
  28. Doug Kolmar 
  29. dkmar@panix.com 
  30.  
  31. FORTRAN: 
  32.  
  33. FUNCTION I1OVRF(LAST) 
  34. C THE ARGUMENT, LAST, IS THE PREVIOUS VALUE OF THE SEQUENCE 
  35. NEW=0 
  36. C THE VARIABLE K EQUALS ONE-HALF THE NUMBER OF POSSIBLE VALUES 
  37.  K=16 
  38.  L=LAST 
  39. C THE VARIABLE PROBIT EQUALS 1/(NUMBER OF POSSIBLE VALUES) 
  40. PROBIT = .03125 
  41. J=L/K 
  42. IF (J.EQ.1) L=L-K 
  43. U=RAND(0) 
  44. IF (U.LT.PROBIT) J=1-J 
  45. NEW = NEW+J*K 
  46. K=K/2 
  47. PROBIT=PROBIT*2 
  48. IF (K.GE.1) GO TO 20 
  49. I1OVRF=NEW 
  50. RETURN 
  51. END  
  52.  
  53.  
  54. ------------------------------------------------------------------- 
  55. C: 
  56.  
  57. #include<stdlib.h> 
  58. #include<time.h> 
  59.  
  60. int rand(); 
  61. void srand(unsigned seed); 
  62. time_t time(time_t  *tmhold); 
  63.  
  64. main() 
  65. int l,last,j,i; 
  66. float probit, u, k, new; 
  67. unsigned seed; 
  68. time_t *tmhold; 
  69.  
  70. last = 30 (could be any value 0-31); 
  71.    for(i=0;i < 20;++i) 
  72.        { 
  73.     last = new; 
  74.     new=0; 
  75.     k=16; 
  76.     l=last; 
  77.     probit = .03125; 
  78.  
  79.       while(k >= 1) 
  80.     { 
  81.     j = ceil(l/k); 
  82.     if(j=1) l=l-k; 
  83.     seed=time(tmhold); 
  84.     srand(seed); 
  85.     u = rand(); 
  86.     u =1/u; 
  87.     if(u < probit) j = 1-j; 
  88.     new = new+j*k; 
  89.     k = k/2; 
  90.     probit = probit * 2; 
  91.       } 
  92.     printf("new= %f \n ",new); 
  93.     } 
  94.    } 
  95.  
  96.  
  97.  
  98.  
  99.